UIGiftGrowUp = Class("UIGiftGrowUp", UIComponentBase)

function UIGiftGrowUp:OnCreate(id, parent)
    self.activeId = id
    self.parent = parent
    self.drawWrapper = GoWrapper.New()
    self.lihui = self:GetChild("lihui");
    self.lihui2 = self:GetChild("lihui2");
    self.title = self:GetChild("title")
    self.banner = self:GetChild("banner")
    self.bannerChar = self:GetChild("bannerChar")
    self.giftList = self:GetChild("giftList")
    self.buttomBug = self:GetChild("buttomBug")
    self.buyTips = self:GetChild("buyTips")
    self.bannerText = self:GetChild("bannerText")
    self.goBtnItem = self:GetChild("goBtn/Item")
    self.buyController = self:GetController("buy")
    self.bannerTips = {};
    for i = 1, 8 do
        table.insert(self.bannerTips, self:GetChild("bannerTips"..i));
    end
    self:RegisterOnClick(self:GetChild("btn_QuickGet"),self.ClickOneReward);
    self:RegisterOnClick(self.buttomBug,self.ClickBuy)
    self:RegisterEvent(Config.EventType.Fresh_GiftGrowUp,self.Refresh)
end
function UIGiftGrowUp:Refresh()
    self.serverData, self.itemCardData, self.modelTableData = logicMgr.CostManager.getCostData(self.activeId);
    logicMgr.CostManager.updateCostHelp(self, self:GetChild("help"), self.modelTableData);
    self.bannerChar.url = tostring(self.modelTableData.f_BannerHead);
    self.banner.url = tostring(self.modelTableData.f_Banner);
    for k, v in pairs(decodeJsonStrToTabel(self.modelTableData.f_BannerTipsShow)) do
        if self.bannerTips[k] ~= nil then
            if v ~= "" then
                self.bannerTips[k].url = tostring(v);
            else
                self.bannerTips[k].url = "";
            end
        else
            break;
        end
    end
    self.title.text = getLanguage(self.serverData.title);
    self.bannerText.text = getLanguage(self.modelTableData.f_Tips);
    if self.serverData.buyCount <= 0 then
        self.buyController.selectedIndex = 0;
        self.buttomBug.title = "￥"..self.serverData.money;
    else
        self.buyController.selectedIndex = 1;
    end
    self.canReward = nil;
    self.giftList:RemoveChildrenToPool();
    --logicMgr.CostManager.updateActiveReceiveStatus(self.severData.items, self.activeId, {1});
    for k, v in ipairs(self.serverData.items) do
        local item = self.giftList:AddItemFromPool()
        self:updateItem(item, v.subId, v,k);
    end
    if #self.serverData.items > 0 then
        local _laskServerData = self.serverData.items[#self.serverData.items];
        self.buyTips.text = string.format(getLanguage(self.modelTableData.f_Tips), _laskServerData.chapter);
        local _tableData = logicMgr.CostManager.findCostItem(self.itemCardData, _laskServerData.subId);
        if _tableData then
            logicMgr.CostManager.RefreshItem(self.goBtnItem, decodeJsonStrToTabel(_tableData.f_Reward[1]));
        end
    end
    for k,v in ipairs(self.serverData.items)do
        if v.receiveState==1 or (v.receiveState==3 and self.serverData.buyCount>0) then
            HelperFunc.ListTranstionPlay(self.giftList, function ()
                self.giftList:ScrollToView(k - 1, true, true);
            end);
            break
        end
    end

    self.parent:updateCostShowByTableOrJson(self.modelTableData.f_ResourceShow);
    logicMgr.CostManager.updateLiHui(self, self.drawWrapper, self.lihui, self.lihui2, self.modelTableData.f_HeroSpine)
end

function UIGiftGrowUp:updateItem(obj, key, serverData, index)
    local tableData = logicMgr.CostManager.findCostItem(self.itemCardData, key);
    if tableData == nil then
        obj.visible = false;
        return ;
    end
    obj.visible = true;
    local diamond = obj:GetChild("list");
    local freeDiamond=obj:GetChild("listFree")
    local title = obj:GetChild("title");
    local receive = obj:GetChild("receive");
    local buyNum = obj:GetChild("buyNum");
    local stateController = obj:GetController("state");
    local isbuyController = obj:GetController("isbuy");
    local upController=obj:GetController("progressUp")
    local downController=obj:GetController("progressDown")
    local upBG=obj:GetChild("upBG")
    local downBG=obj:GetChild("downBG")
    upBG.visible=true
    downBG.visible=true
    upController.selectedIndex=0
    downController.selectedIndex=0
    if self.serverData.nowChapter< serverData.chapter then

    elseif self.serverData.nowChapter==serverData.chapter then
        if index~=1 then
            upController.selectedIndex=1
        end
    else
        if index~=1 then
            upController.selectedIndex=1
        end
        downController.selectedIndex=1
    end
    if index==1 then
        upController.selectedIndex=0
        upBG.visible=false
    elseif index ==#self.serverData.items then
        downController.selectedIndex=0
        downBG.visible=false
    end
    --if severData.progress < severData.chapter then
    --    buyNum.text = "(" ..string.TextColor(severData.progress, "#ff2a4d")..string.format("/%d)", severData.chapter);
    --else
    --    buyNum.text = "(" ..string.TextColor(severData.progress, "#a0c776")..string.format("/%d)", severData.chapter);
    --end
    title.text = string.format(getLanguage(tableData.f_Title), serverData.chapter);
    if self.serverData.buyCount <= 0 then
        isbuyController.selectedIndex = 1;
    else
        isbuyController.selectedIndex = 0;
    end
    receive.onClick:Clear();
    stateController.selectedIndex = serverData.receiveState
    if serverData.receiveState == 0 then
        receive.onClick:Add(function() UISysTips.AddMsg(string.format(getLanguage("GiftGrowUpTips2"), serverData.chapter)); end);
        self:RefreshItemList(freeDiamond,decodeJsonStrToTabel(tableData.f_FreeReward),3)
        self:RefreshItemList(diamond,decodeJsonStrToTabel(tableData.f_Reward),3)
    elseif serverData.receiveState == 3 then
        if self.serverData.buyCount <= 0 then
            self:RefreshItemList(diamond,decodeJsonStrToTabel(tableData.f_Reward),3)
            receive.onClick:Add(function()
                --UISysTips.AddMsg(string.format(getLanguage("GiftGrowUpTips1"), getLanguage(self.serverData.title)));
                self:ClickBuy()
            end);
        else
            stateController.selectedIndex = 4;
            self.canReward = true;
            receive.onClick:Add(function() self:ClickReward(key); end);
            self:RefreshItemList(diamond,decodeJsonStrToTabel(tableData.f_Reward),1)
        end
        self:RefreshItemList(freeDiamond,decodeJsonStrToTabel(tableData.f_FreeReward),2)
    elseif serverData.receiveState == 1 then
        receive.onClick:Add(function() self:ClickReward(key); end);
        self.canReward = true;
        if self.serverData.buyCount <= 0 then
            self:RefreshItemList(diamond,decodeJsonStrToTabel(tableData.f_Reward),3)
        else
            stateController.selectedIndex = 5;
            self:RefreshItemList(diamond,decodeJsonStrToTabel(tableData.f_Reward),1)
        end
        self:RefreshItemList(freeDiamond,decodeJsonStrToTabel(tableData.f_FreeReward),1)
    elseif serverData.receiveState == 2 then
        self:RefreshItemList(freeDiamond,decodeJsonStrToTabel(tableData.f_FreeReward),2)
        self:RefreshItemList(diamond,decodeJsonStrToTabel(tableData.f_Reward),2)
    end
    --logicMgr.CostManager.RefreshItemList(diamond, decodeJsonStrToTabel(tableData.f_Reward), 1);
    --logicMgr.CostManager.RefreshItemList(freeDiamond, decodeJsonStrToTabel(tableData.f_FreeReward), 1);
end
function UIGiftGrowUp:RefreshItemList(list,data,state,showNum)
    if showNum==nil then
        showNum=1
    end
    list:RemoveChildrenToPool()
    for i,v in ipairs(data)do
        local item=list:AddItemFromPool()
        logicMgr.CostManager.RefreshItem(item:GetChild("item"), data[i]);
        item:GetController("start").selectedIndex = state;
    end
    if #data > showNum then
        list.scrollPane.touchEffect = true;
    else
        list.scrollPane.touchEffect = false;
    end
end
function UIGiftGrowUp:ClickBuy()
    showUI(PanelNames.UIGiftGrowUpTips,self.activeId)
    --logicMgr.CostManager.getRewardByIdAndIndex(self.activeId, self.serverData.subId, self.serverData.payId, 1, self.serverData.money, self.onBuySuccess, self);
end

function UIGiftGrowUp:ClickOneReward()
    if self.canReward then
        self:ClickReward(0);
    else
        UISysTips.AddMsg(getLanguage("TaskInfo_btnQFtips"));
    end
end

function UIGiftGrowUp:ClickReward(id)
    local data = {};
    table.insert(data, self.serverData.subId);
    table.insert(data, id);
    if self.serverData.buyCount <= 0 and self.serverData.curLeftRewardNum == 1 then
        local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop)
        if self.serverData.preTitle == "" then
            pop.Init(120, function(flag)
                if flag then
                    logicMgr.CostManager.getRewardById(self.activeId, data, self.onBuySuccess, self);
                end
            end);
        else
            pop.Init(119, function(flag)
                if flag then
                    logicMgr.CostManager.getRewardById(self.activeId, data, self.onBuySuccess, self);
                end
            end, getLanguage(self.serverData.title), getLanguage(self.serverData.preTitle));
        end
    else
        logicMgr.CostManager.getRewardById(self.activeId, data, self.onBuySuccess, self);
    end
end

function UIGiftGrowUp:onBuySuccess(id, index)
end

-- 卸载
function UIGiftGrowUp:OnDestroy()
    self:UnRegisterEvent(Config.EventType.Fresh_GiftGrowUp,self.Refresh)
    if self.RoleLihui then
        destroyImmediate(self.RoleLihui)
        self.RoleLihui = nil
    end
    if self._package and UIPackage.GetById(self._package) then
        UIPackage.RemovePackage(self._package)
    end
end